﻿Imports System.Xml.Serialization

Namespace Commands

    Public Class GetSharedItemDetails
        Friend Shared ReadOnly RequestCode As String = "GetSharedItemDetails"
        Friend Shared ReadOnly ReplyCode As String = "GetSharedItemDetailsReply"
        Private Sub New()
        End Sub

        Public Class Request
            <XmlAttribute()> Public SharedItemId As Integer
        End Class
        Public Class Reply
            <XmlAttribute()> Public InReplyToMessageId As Integer
            Public Item As Entities.SharedItem
            Public Files As Generic.List(Of Entities.SharedItemFile)
            Public MediaInfo As Entities.MediaInfo
        End Class

        Friend Shared Function SendRequest(ByVal client As NetNode.Client, ByVal sharedItemId As Integer) As Integer
            Dim clsRequest As New Request
            clsRequest.SharedItemId = sharedItemId
            Dim strMessage As String = RequestCode
            Dim aData() As Byte = Utils.Serialization.ToXmlBytes(clsRequest)
            Return client.SendMessageToTargetAsync(Manager.TargetName, strMessage, aData)
        End Function

        Friend Shared Sub HandleRequest(ByVal client As NetNode.Client, ByVal message As NetNode.MessageBlock)
            Dim clsReq As Request = Utils.Serialization.FromXmlBytes(Of Request)(message.BinaryData)

            Dim clsReply As New Reply
            clsReply.InReplyToMessageId = message.Id

            clsReply.Item = DataStore.SharedItems.GetById(clsReq.SharedItemId)
            clsReply.Files = DataStore.SharedItemsFiles.GetAllBySharedItemId(clsReq.SharedItemId)
            clsReply.MediaInfo = DataStore.MediaInfo.GetBySharedItemId(clsReq.SharedItemId)

            Dim bData() As Byte = Utils.Serialization.ToXmlBytes(clsReply)
            client.SendMessageToTargetAsync(Manager.TargetName, ReplyCode, bData)
        End Sub

        Friend Shared Sub HandleReply(ByVal client As NetNode.Client, ByVal message As NetNode.MessageBlock)
            Utils.Threading.Invoke(AddressOf HandleReply_CallBack, client, message)
        End Sub
        Private Shared Sub HandleReply_CallBack(ByVal client As NetNode.Client, ByVal message As NetNode.MessageBlock)
            Dim clsReply As Reply = Utils.Serialization.FromXmlBytes(Of Reply)(message.BinaryData)
            Manager.OnGetSharedItemDetailsReply(client, clsReply)
        End Sub
    End Class

End Namespace
